package org.example.test.test;

/**
 * 最后一个单词的长度
 */
public class LengthOfLastWord {

    public static void main(String[] args) {
        lengthOfLastWord2("a");
    }

    public int lengthOfLastWord(String s) {
        String[] split = s.trim().split(" ");
        String s1 = split[split.length - 1];
        return s1.length();
    }

    public static int lengthOfLastWord2(String s) {
        int end = s.length() - 1;
        while (end > 0 && s.charAt(end) == ' ') {
            end--;
        }
        int start = end;
        while (start >= 0 && s.charAt(start) != ' '){
            start --;
        }
        return end - start;
    }
}
